package haust;
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.lwjgl.opengl.DisplayMode;
 
/**
 * A very simple options splash screen where you choose resolution, fullscreen and 3D options.
 */
@SuppressWarnings("serial")
public class SplashScreen extends JPanel
                          implements ActionListener {
    private JFrame frame;
    private boolean fullscreen = false;
    private boolean cancel = false;
    private boolean option3D = false;
    private int selIndex = 0;
    private boolean exit = false;
 
    
    /**
     * 
     * @param dm Array of displaymodes acquired from lwjgl.opengl.Display
     */
    public SplashScreen(DisplayMode [] dm) {
        super(new BorderLayout());
         frame = new JFrame("Options Dialog");
 
        String[] strs = new String[dm.length];    	
    	for( int i = 0; i < dm.length; ++i )
    		strs[i] = dm[i].getWidth() + "x" + dm[i].getHeight() + "x" + dm[i].getBitsPerPixel();

 
    	//Initialize windowcomponents
        JComponent newContentPane = this;
        newContentPane.setOpaque(true); 
        frame.setContentPane(newContentPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
        JComboBox listbox = new JComboBox(strs);
        listbox.setSelectedIndex(0);

        JButton btnOk = new JButton("OK");
        JButton btnCancel = new JButton("Cancel");
        JCheckBoxMenuItem checkBox = new JCheckBoxMenuItem("3D");
        JCheckBoxMenuItem checkBox2 = new JCheckBoxMenuItem("Fullscreen");

        add(listbox, BorderLayout.NORTH  );
        add(btnOk, BorderLayout.SOUTH);        
        add(btnCancel, BorderLayout.EAST);        
        add(checkBox, BorderLayout.CENTER);
        add(checkBox2, BorderLayout.WEST);
        
        btnOk.addActionListener(this);
        btnCancel.addActionListener(this);
        checkBox.addActionListener(this);
        listbox.addActionListener(this);
        checkBox2.addActionListener(this);
       
        //Display the window.        
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        frame.pack();
        frame.setVisible(true);
    }
 
    /**
     * 
     */
    public void actionPerformed(ActionEvent e) {
    	if( e.getActionCommand().equals("Cancel") )
    	{
    		exit = true;
    		cancel = true;
    	}
    	else if( e.getActionCommand().equals("OK" ))
    		exit = true;
    	else if( e.getActionCommand().equals("comboBoxChanged"))
    	{
            JComboBox cb = (JComboBox)e.getSource();
            selIndex = cb.getSelectedIndex();
    	}
    	else if( e.getActionCommand().equals("3D"))
    		option3D = !option3D;
    	else if( e.getActionCommand().equals("Fullscreen"))
    		fullscreen = !fullscreen;
    }
 
    /**
     * Destroys the window and its associated children
     */
    public void destroy() 
    {
    	frame.dispose();
    }

    
    public boolean getFullscreen() { return fullscreen; }
    public int getSelectionIndex() { return selIndex; }
    public boolean getOption3D() { return option3D; }
    public boolean getCancelled() { return cancel; }
    public boolean getExitFlag() { return exit; }
}